package com.mvvm.demo.ui.detail

import android.os.Build
import android.os.Bundle
import android.webkit.*
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.mvvm.base.base.view.activity.BaseActivity
import com.mvvm.demo.R
import kotlinx.android.synthetic.main.activity_detail.*
@Route(path = "/app/detail/")
class DetailActivity : BaseActivity() {

    override fun initView(savedInstanceState: Bundle?) {
        initWebView()
        intent.getStringExtra("url")?.let {
            wv_detail.loadUrl(it)
        }
        wv_detail.loadUrl("https://www.baidu.com/")
    }



    private fun initWebView() {
        wv_detail.setInitialScale(100)
        wv_detail.webViewClient = webViewClient
        val ws = wv_detail.settings
        with(ws) {
            loadWithOverviewMode = false
            setSupportZoom(true)
            builtInZoomControls = true
            displayZoomControls = false
            setAppCacheEnabled(true)
            cacheMode = WebSettings.LOAD_DEFAULT
            useWideViewPort = true
            blockNetworkImage = false
            domStorageEnabled = true
            layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            textZoom = 100
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
    }

    private val webViewClient = object : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            view?.loadUrl(url)
            return true
        }

        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            view?.loadUrl(request?.url.toString())
            return true
        }
    }
    override val layoutId: Int
        get() = R.layout.activity_detail

    override fun onDestroy() {
        WebStorage.getInstance().deleteAllData();
        wv_detail.clearCache(true)
        val cm = CookieManager.getInstance()
        cm.removeSessionCookies(object : ValueCallback<Boolean> {
            override fun onReceiveValue(value: Boolean?) {

            }
        })
        cm.removeSessionCookies(object : ValueCallback<Boolean> {
            override fun onReceiveValue(value: Boolean?) {

            }
        })
        cm.flush()
        wv_detail.destroy()
        super.onDestroy()

    }

}
